package com.stylefeng.guns.modular.deviceTime.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.stylefeng.HCNetSDK;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.base.tips.ErrorTip;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.modular.deviceTime.service.IDeviceTimeService;
import com.stylefeng.guns.modular.doorDevice.service.IDeviceService;
import com.stylefeng.guns.modular.system.model.Device;
import com.stylefeng.guns.modular.system.model.DeviceTime;
import com.sun.jna.NativeLong;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.IntByReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 配置时间段控制器
 *
 * @author fengshuonan
 * @Date 2018-10-08 17:38:06
 */
@Controller
@RequestMapping("/deviceTime")
public class DeviceTimeController extends BaseController {

    private String PREFIX = "/deviceTime/deviceTime/";

    static HCNetSDK hCNetSDK= HCNetSDK.INSTANCE;

    @Autowired
    private IDeviceTimeService deviceTimeService;

    @Autowired
    private IDeviceService deviceService;


    /**
     * 跳转到配置时间段首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "deviceTime.html";
    }

    /**
     * 跳转到添加配置时间段
     */
    @RequestMapping("/deviceTime_add")
    public String deviceTimeAdd() {
        return PREFIX + "deviceTime_add.html";
    }

    /**
     * 跳转到修改配置时间段
     */
    @RequestMapping("/deviceTime_update/{deviceTimeId}")
    public String deviceTimeUpdate(@PathVariable Integer deviceTimeId, Model model) {
        DeviceTime deviceTime = deviceTimeService.selectById(deviceTimeId);
        model.addAttribute("item",deviceTime);
        LogObjectHolder.me().set(deviceTime);
        return PREFIX + "deviceTime_edit.html";
    }

    /**
     * 获取配置时间段列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String name,String device,String week) {
        System.out.println(name);
        EntityWrapper<DeviceTime> entityWrapper=new EntityWrapper<>();
        Wrapper<DeviceTime> wrapper=entityWrapper.like("name",name).like("device",device).like("week",week);
        return deviceTimeService.selectList(wrapper);
    }

    /**
     * 新增配置时间段
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(DeviceTime deviceTime) {
        hCNetSDK.NET_DVR_Init();
        hCNetSDK.NET_DVR_SetConnectTime(2000,1);
        hCNetSDK.NET_DVR_SetReconnect(10000,true);
        HCNetSDK.NET_DVR_DEVICEINFO_V30 m_strDeviceInfo=new HCNetSDK.NET_DVR_DEVICEINFO_V30();
        NativeLong lUserID;
        EntityWrapper<Device> entityWrapper=new EntityWrapper<>();
        Wrapper<Device> wrapper=entityWrapper.like("name",deviceTime.getDevice());
        Device device=deviceService.selectOne(wrapper);
        short port=Short.parseShort(device.getPort());
        lUserID=hCNetSDK.NET_DVR_Login_V30(device.getIp(), port,device.getAccount(),device.getPassword(),m_strDeviceInfo);
        int code=setTime(lUserID,deviceTime);
        if(code==1900){
            return  ERROR_TIP;
        }
        deviceTimeService.insert(deviceTime);
        return SUCCESS_TIP;
    }

    /**
     * 删除配置时间段
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer deviceTimeId) {
        hCNetSDK.NET_DVR_Init();
        hCNetSDK.NET_DVR_SetConnectTime(2000,1);
        hCNetSDK.NET_DVR_SetReconnect(10000,true);
        HCNetSDK.NET_DVR_DEVICEINFO_V30 m_strDeviceInfo=new HCNetSDK.NET_DVR_DEVICEINFO_V30();
        NativeLong lUserID;
        EntityWrapper<Device> entityWrapper=new EntityWrapper<>();
        DeviceTime deviceTime=deviceTimeService.selectById(deviceTimeId);
        Wrapper<Device> wrapper=entityWrapper.like("name",deviceTime.getDevice());
        Device device=deviceService.selectOne(wrapper);
        short port=Short.parseShort(device.getPort());
        lUserID=hCNetSDK.NET_DVR_Login_V30(device.getIp(), port,device.getAccount(),device.getPassword(),m_strDeviceInfo);
        delTime(lUserID,deviceTime);
        deviceTimeService.deleteById(deviceTimeId);
        return SUCCESS_TIP;
    }

    /**
     * 修改配置时间段
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(DeviceTime deviceTime) {
        hCNetSDK.NET_DVR_Init();
        hCNetSDK.NET_DVR_SetConnectTime(2000,1);
        hCNetSDK.NET_DVR_SetReconnect(10000,true);
        HCNetSDK.NET_DVR_DEVICEINFO_V30 m_strDeviceInfo=new HCNetSDK.NET_DVR_DEVICEINFO_V30();
        NativeLong lUserID;
        EntityWrapper<Device> entityWrapper=new EntityWrapper<>();
        Wrapper<Device> wrapper=entityWrapper.like("name",deviceTime.getDevice());
        Device device=deviceService.selectOne(wrapper);
        short port=Short.parseShort(device.getPort());
        lUserID=hCNetSDK.NET_DVR_Login_V30(device.getIp(), port,device.getAccount(),device.getPassword(),m_strDeviceInfo);
        int code=updateTime(lUserID,deviceTime);
        if(code==1900){
            return  ERROR_TIP;
        }
        deviceTimeService.updateById(deviceTime);
        return SUCCESS_TIP;
    }

    /**
     * 配置时间段详情
     */
    @RequestMapping(value = "/detail/{deviceTimeId}")
    @ResponseBody
    public Object detail(@PathVariable("deviceTimeId") Integer deviceTimeId) {
        return deviceTimeService.selectById(deviceTimeId);
    }

    @RequestMapping(value = "/status")
    @ResponseBody
    public Object week() {
        List<Device> deviceList=deviceService.selectList(null);
        return  deviceList;
    }

    /**
     * 跳转到重置时间段页面
     */
    @RequestMapping("/deviceTime_reset")
    public String deviceTimeReset() {
        return PREFIX + "deviceTime_reset.html";
    }

    /**
     * 重置所有时间段
     */
    @RequestMapping(value = "/reset")
    @ResponseBody
    public Object resetTime(DeviceTime deviceTime){
        hCNetSDK.NET_DVR_Init();
        hCNetSDK.NET_DVR_SetConnectTime(2000,1);
        hCNetSDK.NET_DVR_SetReconnect(10000,true);
        HCNetSDK.NET_DVR_DEVICEINFO_V30 m_strDeviceInfo=new HCNetSDK.NET_DVR_DEVICEINFO_V30();
        NativeLong lUserID;
        EntityWrapper<Device> entityWrapper=new EntityWrapper<>();
        Wrapper<Device> wrapper=entityWrapper.like("name",deviceTime.getDevice());
        Device device=deviceService.selectOne(wrapper);
        short port=Short.parseShort(device.getPort());
        lUserID=hCNetSDK.NET_DVR_Login_V30(device.getIp(), port,device.getAccount(),device.getPassword(),m_strDeviceInfo);
        reset(lUserID);
        EntityWrapper<DeviceTime> entityWrapper2=new EntityWrapper<>();
        Wrapper<DeviceTime> wrapper2=entityWrapper2.eq("device",deviceTime.getDevice());
        deviceTimeService.delete(wrapper2);
        return SUCCESS_TIP;


    }

    public int setTime(NativeLong lUserID, DeviceTime deviceTime){
        int iErr = 0;

        HCNetSDK.NET_DVR_WEEK_PLAN_COND struWeekPlanCond = new HCNetSDK.NET_DVR_WEEK_PLAN_COND();
        struWeekPlanCond.dwSize = struWeekPlanCond.size();
        struWeekPlanCond.dwWeekPlanNumber  = 1;
        struWeekPlanCond.wLocalControllerID = 0;

        HCNetSDK.NET_DVR_WEEK_PLAN_CFG struWeekPlanCfg = new HCNetSDK.NET_DVR_WEEK_PLAN_CFG();

        IntByReference pInt = new IntByReference(0);
        Pointer lpStatusList = pInt.getPointer();

        struWeekPlanCond.write();
        struWeekPlanCfg.write();

        Pointer lpCond = struWeekPlanCond.getPointer();
        Pointer lpInbuferCfg = struWeekPlanCfg.getPointer();

        if (false == hCNetSDK.NET_DVR_GetDeviceConfig(lUserID, HCNetSDK.NET_DVR_GET_CARD_RIGHT_WEEK_PLAN_V50, 1, lpCond, struWeekPlanCond.size(), lpStatusList, lpInbuferCfg, struWeekPlanCfg.size()))
        {
            iErr = hCNetSDK.NET_DVR_GetLastError();
            System.out.println("NET_DVR_GET_CARD_RIGHT_WEEK_PLAN_V50失败" );
            return -1;
        }
        struWeekPlanCfg.read();

       /* for(int i=0;i<7;i++)
        {
            for(int j=0;j<8;j++)
            {
                struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[j].byEnable = 0;
                struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[j].struTimeSegment.struBeginTime.byHour = 0;
                struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[j].struTimeSegment.struBeginTime.byMinute = 0;
                struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[j].struTimeSegment.struBeginTime.bySecond = 0;
                struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[j].struTimeSegment.struEndTime.byHour = 0;
                struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[j].struTimeSegment.struEndTime.byMinute = 0;
                struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[j].struTimeSegment.struEndTime.bySecond = 0;
            }
        }*/
        EntityWrapper<DeviceTime> entityWrapper=new EntityWrapper<>();
        Wrapper<DeviceTime> wrapper=entityWrapper.like("week",deviceTime.getWeek().toString()).eq("device",deviceTime.getDevice()).orderBy("groupee",true);
        int count=0;
        List<DeviceTime> list=deviceTime.selectList(wrapper);
        if(list.size()==0){
            count=0;
        }
        else{
            for(DeviceTime deTime:list){
                if(count==deTime.getGroupee()){
                    count++;
                }
            }
        }
        deviceTime.setGroupee(count);


        Byte sta;
        if(deviceTime.getStatus()==1){
           sta=1;
        }
        else{
            sta=0;
        }
        String begin=deviceTime.getBegin();
        String[] bstr=begin.split(":");
        int bhour=Integer.parseInt(bstr[0]);
        int bmin=Integer.parseInt(bstr[1]);
        int bse=Integer.parseInt(bstr[2]);
        Byte bhr=(byte)bhour;
        Byte bm=(byte)bmin;
        Byte bs=(byte)bse;
        String end=deviceTime.getEnd();
        System.out.println(deviceTime.getEnd());
        String[] estr=end.split(":");
        int ehour=Integer.parseInt(estr[0]);
        int emin=Integer.parseInt(estr[1]);
        int ese=Integer.parseInt(estr[2]);
        Byte ehr=(byte)ehour;
        Byte em=(byte)emin;
        Byte es=(byte)ese;
        System.out.println("第几组AAAAAAAAAAA"+count);
        struWeekPlanCfg.struPlanCfg[deviceTime.getWeek()-1].struPlanCfgDay[count].byEnable = sta;
        struWeekPlanCfg.struPlanCfg[deviceTime.getWeek()-1].struPlanCfgDay[count].struTimeSegment.struBeginTime.byHour = bhr;
        struWeekPlanCfg.struPlanCfg[deviceTime.getWeek()-1].struPlanCfgDay[count].struTimeSegment.struBeginTime.byMinute = bm;
        struWeekPlanCfg.struPlanCfg[deviceTime.getWeek()-1].struPlanCfgDay[count].struTimeSegment.struBeginTime.bySecond = bs;
        struWeekPlanCfg.struPlanCfg[deviceTime.getWeek()-1].struPlanCfgDay[count].struTimeSegment.struEndTime.byHour = ehr;
        struWeekPlanCfg.struPlanCfg[deviceTime.getWeek()-1].struPlanCfgDay[count].struTimeSegment.struEndTime.byMinute = em;
        struWeekPlanCfg.struPlanCfg[deviceTime.getWeek()-1].struPlanCfgDay[count].struTimeSegment.struEndTime.bySecond = es;
        struWeekPlanCfg.write();

        if (false == hCNetSDK.NET_DVR_SetDeviceConfig(lUserID, HCNetSDK.NET_DVR_SET_CARD_RIGHT_WEEK_PLAN_V50, 1, lpCond, struWeekPlanCond.size(), lpStatusList, lpInbuferCfg, struWeekPlanCfg.size()))
        {
            iErr = hCNetSDK.NET_DVR_GetLastError();
            System.out.println( "NET_DVR_SET_CARD_RIGHT_WEEK_PLAN_V50失败，错误号：" + iErr);
            return iErr;
        }
        System.out.println("NET_DVR_SET_CARD_RIGHT_WEEK_PLAN_V50成功！");
        hCNetSDK.NET_DVR_Logout(lUserID);
        hCNetSDK.NET_DVR_Cleanup();
        return iErr;
    }
    public int updateTime(NativeLong lUserID, DeviceTime deviceTime){
        int iErr = 0;

        HCNetSDK.NET_DVR_WEEK_PLAN_COND struWeekPlanCond = new HCNetSDK.NET_DVR_WEEK_PLAN_COND();
        struWeekPlanCond.dwSize = struWeekPlanCond.size();
        struWeekPlanCond.dwWeekPlanNumber  = 1;
        struWeekPlanCond.wLocalControllerID = 0;

        HCNetSDK.NET_DVR_WEEK_PLAN_CFG struWeekPlanCfg = new HCNetSDK.NET_DVR_WEEK_PLAN_CFG();

        IntByReference pInt = new IntByReference(0);
        Pointer lpStatusList = pInt.getPointer();

        struWeekPlanCond.write();
        struWeekPlanCfg.write();

        Pointer lpCond = struWeekPlanCond.getPointer();
        Pointer lpInbuferCfg = struWeekPlanCfg.getPointer();

        if (false == hCNetSDK.NET_DVR_GetDeviceConfig(lUserID, HCNetSDK.NET_DVR_GET_CARD_RIGHT_WEEK_PLAN_V50, 1, lpCond, struWeekPlanCond.size(), lpStatusList, lpInbuferCfg, struWeekPlanCfg.size()))
        {
            iErr = hCNetSDK.NET_DVR_GetLastError();
            System.out.println("NET_DVR_GET_CARD_RIGHT_WEEK_PLAN_V50失败" );
            return iErr;
        }
        struWeekPlanCfg.read();

       /* for(int i=0;i<7;i++)
        {
            for(int j=0;j<8;j++)
            {
                struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[j].byEnable = 0;
                struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[j].struTimeSegment.struBeginTime.byHour = 0;
                struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[j].struTimeSegment.struBeginTime.byMinute = 0;
                struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[j].struTimeSegment.struBeginTime.bySecond = 0;
                struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[j].struTimeSegment.struEndTime.byHour = 0;
                struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[j].struTimeSegment.struEndTime.byMinute = 0;
                struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[j].struTimeSegment.struEndTime.bySecond = 0;
            }
        }*/


        Byte sta;
        if(deviceTime.getStatus()==1){
            sta=1;
        }
        else{
            sta=0;
        }
        String begin=deviceTime.getBegin();
        String[] bstr=begin.split(":");
        int bhour=Integer.parseInt(bstr[0]);
        int bmin=Integer.parseInt(bstr[1]);
        int bse=Integer.parseInt(bstr[2]);
        Byte bhr=(byte)bhour;
        Byte bm=(byte)bmin;
        Byte bs=(byte)bse;
        String end=deviceTime.getEnd();
        System.out.println(deviceTime.getEnd());
        String[] estr=end.split(":");
        int ehour=Integer.parseInt(estr[0]);
        int emin=Integer.parseInt(estr[1]);
        int ese=Integer.parseInt(estr[2]);
        Byte ehr=(byte)ehour;
        Byte em=(byte)emin;
        Byte es=(byte)ese;
        System.out.println(deviceTime.getGroupee());
        DeviceTime deviceTime1=deviceTimeService.selectById(deviceTime.getId());
        int group=deviceTime1.getGroupee();
        System.out.println("第几组AAAAAAAAAAA"+group);
        struWeekPlanCfg.struPlanCfg[deviceTime.getWeek()-1].struPlanCfgDay[group].byEnable = sta;
        struWeekPlanCfg.struPlanCfg[deviceTime.getWeek()-1].struPlanCfgDay[group].struTimeSegment.struBeginTime.byHour = bhr;
        struWeekPlanCfg.struPlanCfg[deviceTime.getWeek()-1].struPlanCfgDay[group].struTimeSegment.struBeginTime.byMinute = bm;
        struWeekPlanCfg.struPlanCfg[deviceTime.getWeek()-1].struPlanCfgDay[group].struTimeSegment.struBeginTime.bySecond = bs;
        struWeekPlanCfg.struPlanCfg[deviceTime.getWeek()-1].struPlanCfgDay[group].struTimeSegment.struEndTime.byHour = ehr;
        struWeekPlanCfg.struPlanCfg[deviceTime.getWeek()-1].struPlanCfgDay[group].struTimeSegment.struEndTime.byMinute = em;
        struWeekPlanCfg.struPlanCfg[deviceTime.getWeek()-1].struPlanCfgDay[group].struTimeSegment.struEndTime.bySecond = es;
        struWeekPlanCfg.write();

        if (false == hCNetSDK.NET_DVR_SetDeviceConfig(lUserID, HCNetSDK.NET_DVR_SET_CARD_RIGHT_WEEK_PLAN_V50, 1, lpCond, struWeekPlanCond.size(), lpStatusList, lpInbuferCfg, struWeekPlanCfg.size()))
        {
            iErr = hCNetSDK.NET_DVR_GetLastError();
            System.out.println( "NET_DVR_SET_CARD_RIGHT_WEEK_PLAN_V50失败，错误号：" + iErr);
            return iErr;
        }
        System.out.println("NET_DVR_SET_CARD_RIGHT_WEEK_PLAN_V50成功！");
        hCNetSDK.NET_DVR_Logout(lUserID);
        hCNetSDK.NET_DVR_Cleanup();
        return iErr;
    }
    public void reset(NativeLong lUserID){
        int iErr = 0;

        HCNetSDK.NET_DVR_WEEK_PLAN_COND struWeekPlanCond = new HCNetSDK.NET_DVR_WEEK_PLAN_COND();
        struWeekPlanCond.dwSize = struWeekPlanCond.size();
        struWeekPlanCond.dwWeekPlanNumber  = 1;
        struWeekPlanCond.wLocalControllerID = 0;

        HCNetSDK.NET_DVR_WEEK_PLAN_CFG struWeekPlanCfg = new HCNetSDK.NET_DVR_WEEK_PLAN_CFG();

        IntByReference pInt = new IntByReference(0);
        Pointer lpStatusList = pInt.getPointer();

        struWeekPlanCond.write();
        struWeekPlanCfg.write();

        Pointer lpCond = struWeekPlanCond.getPointer();
        Pointer lpInbuferCfg = struWeekPlanCfg.getPointer();

        if (false == hCNetSDK.NET_DVR_GetDeviceConfig(lUserID, HCNetSDK.NET_DVR_GET_CARD_RIGHT_WEEK_PLAN_V50, 1, lpCond, struWeekPlanCond.size(), lpStatusList, lpInbuferCfg, struWeekPlanCfg.size()))
        {
            iErr = hCNetSDK.NET_DVR_GetLastError();
            System.out.println("NET_DVR_GET_CARD_RIGHT_WEEK_PLAN_V50失败" );
            return;
        }
        struWeekPlanCfg.read();
        System.out.println(struWeekPlanCond.dwWeekPlanNumber);

        for(int i=0;i<7;i++)
        {
            for(int j=0;j<8;j++)
            {
                struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[j].byEnable = 0;
                struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[j].struTimeSegment.struBeginTime.byHour = 0;
                struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[j].struTimeSegment.struBeginTime.byMinute = 0;
                struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[j].struTimeSegment.struBeginTime.bySecond = 0;
                struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[j].struTimeSegment.struEndTime.byHour = 0;
                struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[j].struTimeSegment.struEndTime.byMinute = 0;
                struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[j].struTimeSegment.struEndTime.bySecond = 0;
            }
        }

        for(int i=0;i<7;i++)
        {
            struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[0].byEnable = 1;
            struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[0].struTimeSegment.struBeginTime.byHour = 0;
            struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[0].struTimeSegment.struBeginTime.byMinute = 0;
            struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[0].struTimeSegment.struBeginTime.bySecond = 0;
            struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[0].struTimeSegment.struEndTime.byHour = 23;
            struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[0].struTimeSegment.struEndTime.byMinute = 59;
            struWeekPlanCfg.struPlanCfg[i].struPlanCfgDay[0].struTimeSegment.struEndTime.bySecond = 59;
        }
        struWeekPlanCfg.write();


        if (false == hCNetSDK.NET_DVR_SetDeviceConfig(lUserID, HCNetSDK.NET_DVR_SET_CARD_RIGHT_WEEK_PLAN_V50, 1, lpCond, struWeekPlanCond.size(), lpStatusList, lpInbuferCfg, struWeekPlanCfg.size()))
        {
            iErr = hCNetSDK.NET_DVR_GetLastError();
            System.out.println( "NET_DVR_SET_CARD_RIGHT_WEEK_PLAN_V50失败，错误号：" + iErr);
            return;
        }
        System.out.println("NET_DVR_SET_CARD_RIGHT_WEEK_PLAN_V50成功！");
        hCNetSDK.NET_DVR_Logout(lUserID);
        hCNetSDK.NET_DVR_Cleanup();
        return;
    }
    public void delTime(NativeLong lUserID,DeviceTime deviceTime){
        int iErr = 0;

        HCNetSDK.NET_DVR_WEEK_PLAN_COND struWeekPlanCond = new HCNetSDK.NET_DVR_WEEK_PLAN_COND();
        struWeekPlanCond.dwSize = struWeekPlanCond.size();
        struWeekPlanCond.dwWeekPlanNumber  = 1;
        struWeekPlanCond.wLocalControllerID = 0;

        HCNetSDK.NET_DVR_WEEK_PLAN_CFG struWeekPlanCfg = new HCNetSDK.NET_DVR_WEEK_PLAN_CFG();

        IntByReference pInt = new IntByReference(0);
        Pointer lpStatusList = pInt.getPointer();

        struWeekPlanCond.write();
        struWeekPlanCfg.write();

        Pointer lpCond = struWeekPlanCond.getPointer();
        Pointer lpInbuferCfg = struWeekPlanCfg.getPointer();

        if (false == hCNetSDK.NET_DVR_GetDeviceConfig(lUserID, HCNetSDK.NET_DVR_GET_CARD_RIGHT_WEEK_PLAN_V50, 1, lpCond, struWeekPlanCond.size(), lpStatusList, lpInbuferCfg, struWeekPlanCfg.size()))
        {
            iErr = hCNetSDK.NET_DVR_GetLastError();
            System.out.println("NET_DVR_GET_CARD_RIGHT_WEEK_PLAN_V50失败" );
            return;
        }
        struWeekPlanCfg.read();
        System.out.println(struWeekPlanCond.dwWeekPlanNumber);
        int week=deviceTime.getWeek();
        int group=deviceTime.getGroupee();
        System.out.println("删除的是第几组"+group);
                struWeekPlanCfg.struPlanCfg[week-1].struPlanCfgDay[group].byEnable = 0;
                struWeekPlanCfg.struPlanCfg[week-1].struPlanCfgDay[group].struTimeSegment.struBeginTime.byHour = 0;
                struWeekPlanCfg.struPlanCfg[week-1].struPlanCfgDay[group].struTimeSegment.struBeginTime.byMinute = 0;
                struWeekPlanCfg.struPlanCfg[week-1].struPlanCfgDay[group].struTimeSegment.struBeginTime.bySecond = 0;
                struWeekPlanCfg.struPlanCfg[week-1].struPlanCfgDay[group].struTimeSegment.struEndTime.byHour = 0;
                struWeekPlanCfg.struPlanCfg[week-1].struPlanCfgDay[group].struTimeSegment.struEndTime.byMinute = 0;
                struWeekPlanCfg.struPlanCfg[week-1].struPlanCfgDay[group].struTimeSegment.struEndTime.bySecond = 0;

        struWeekPlanCfg.write();

        if (false == hCNetSDK.NET_DVR_SetDeviceConfig(lUserID, HCNetSDK.NET_DVR_SET_CARD_RIGHT_WEEK_PLAN_V50, 1, lpCond, struWeekPlanCond.size(), lpStatusList, lpInbuferCfg, struWeekPlanCfg.size()))
        {
            iErr = hCNetSDK.NET_DVR_GetLastError();
            System.out.println( "NET_DVR_SET_CARD_RIGHT_WEEK_PLAN_V50失败，错误号：" + iErr);
            return;
        }
        System.out.println("NET_DVR_SET_CARD_RIGHT_WEEK_PLAN_V50成功！");
        hCNetSDK.NET_DVR_Logout(lUserID);
        hCNetSDK.NET_DVR_Cleanup();
        return;
    }
    @RequestMapping(value = "/openDoor")
    @ResponseBody
    public Object openDoor(String name,String dwStaic){
        hCNetSDK.NET_DVR_Init();
        hCNetSDK.NET_DVR_SetConnectTime(2000,1);
        hCNetSDK.NET_DVR_SetReconnect(10000,true);
        HCNetSDK.NET_DVR_DEVICEINFO_V30 m_strDeviceInfo=new HCNetSDK.NET_DVR_DEVICEINFO_V30();
        NativeLong lUserID;
        EntityWrapper<Device> entityWrapper=new EntityWrapper<>();
        Wrapper<Device> wrapper=entityWrapper.like("name",name);
        Device device=deviceService.selectOne(wrapper);
        short port=Short.parseShort(device.getPort());
        lUserID=hCNetSDK.NET_DVR_Login_V30(device.getIp(), port,device.getAccount(),device.getPassword(),m_strDeviceInfo);
        NativeLong lGatewayIndex=new NativeLong(-1);
        Boolean boo=hCNetSDK.NET_DVR_ControlGateway(lUserID,lGatewayIndex,Integer.parseInt(dwStaic));
        return SUCCESS_TIP;

    }
    @RequestMapping(value = "/openAllDoor")
    @ResponseBody
    public Object openAllDoor(String dwStaic){
        List<Device> list=deviceService.selectList(null);
        for(Device device:list) {
            NativeLong lUserID;
            hCNetSDK.NET_DVR_Init();
            hCNetSDK.NET_DVR_SetConnectTime(2000, 1);
            hCNetSDK.NET_DVR_SetReconnect(10000, true);
            HCNetSDK.NET_DVR_DEVICEINFO_V30 m_strDeviceInfo = new HCNetSDK.NET_DVR_DEVICEINFO_V30();
            short port = Short.parseShort(device.getPort());
            lUserID = hCNetSDK.NET_DVR_Login_V30(device.getIp(), port, device.getAccount(), device.getPassword(), m_strDeviceInfo);
            System.out.println(lUserID);
            NativeLong lGatewayIndex = new NativeLong(-1);
            Boolean boo = hCNetSDK.NET_DVR_ControlGateway(lUserID, lGatewayIndex,Integer.parseInt(dwStaic));
            hCNetSDK.NET_DVR_Logout(lUserID);
            hCNetSDK.NET_DVR_Cleanup();
        }
        return SUCCESS_TIP;
    }
}
